package org.jeecg.modules.erp.goods.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.jeecg.common.system.util.JwtUtil;
import org.jeecg.modules.erp.goods.entity.ErpGoodsSupplier;
import org.jeecg.modules.erp.goods.mapper.ErpGoodsSupplierMapper;
import org.jeecg.modules.erp.goods.service.IErpGoodsSupplierService;
import org.jeecg.modules.erp.goods.vo.ErpGoodsSupplierVo;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.http.HttpServletRequest;

/**
 * @Description: 物料供应商
 * @Author: jeecg-boot
 * @Date:   2024-06-11
 * @Version: V1.0
 */
@Service
public class ErpGoodsSupplierServiceImpl extends ServiceImpl<ErpGoodsSupplierMapper, ErpGoodsSupplier> implements IErpGoodsSupplierService {
	
	@Autowired
	private ErpGoodsSupplierMapper erpGoodsSupplierMapper;
	
	@Override
	public List<ErpGoodsSupplier> selectByMainId(String mainId) {
		return erpGoodsSupplierMapper.selectByMainId(mainId);
	}

	@Override
	public IPage<ErpGoodsSupplierVo> myPage(Page<ErpGoodsSupplierVo> page, ErpGoodsSupplierVo model) {
		return getBaseMapper().myPage(page, model);
	}

	@Override
	public void addSave(ErpGoodsSupplierVo goodsSupplierVo, HttpServletRequest req) {
		List<String> supplierIds = Arrays.asList(goodsSupplierVo.getIds().split(","));
		for (String supplierId: supplierIds) {
			ErpGoodsSupplier erpGoodsSupplier = new ErpGoodsSupplier();
			erpGoodsSupplier.setId(String.valueOf(IdWorker.getId()));
			erpGoodsSupplier.setGoodsId(goodsSupplierVo.getGoodsId());
			erpGoodsSupplier.setSupplierId(supplierId);
			String username = JwtUtil.getUserNameByToken(req);
			erpGoodsSupplier.setCreateBy(username);
			erpGoodsSupplier.setCreateTime(new Date());
			ErpGoodsSupplier joinSystem = getBaseMapper().findBySupplierId(erpGoodsSupplier);
			if(joinSystem==null) {
				this.save(erpGoodsSupplier);
			}
		}
	}
}
